package com.atguigu.tingshu.order.strategy.impl;

import com.alibaba.fastjson.JSON;
import com.atguigu.tingshu.album.client.AlbumInfoFeignClient;
import com.atguigu.tingshu.common.constant.SystemConstant;
import com.atguigu.tingshu.common.execption.GuiguException;
import com.atguigu.tingshu.common.result.ResultCodeEnum;
import com.atguigu.tingshu.common.util.AuthContextHolder;
import com.atguigu.tingshu.model.album.AlbumInfo;
import com.atguigu.tingshu.model.user.UserInfo;
import com.atguigu.tingshu.order.helper.SignHelper;
import com.atguigu.tingshu.order.strategy.OrderTradeStrategy;
import com.atguigu.tingshu.user.client.UserInfoFeignClient;
import com.atguigu.tingshu.vo.order.OrderDerateVo;
import com.atguigu.tingshu.vo.order.OrderDetailVo;
import com.atguigu.tingshu.vo.order.OrderInfoVo;
import com.atguigu.tingshu.vo.order.TradeVo;
import com.google.common.collect.Lists;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.*;

/**
 * Author  王东剑
 * Created on 21/6/2024 PM6:40
 * Package_name com.atguigu.tingshu.order.strategy.impl
 * Description
 */
@Service
@Slf4j
public class AlbumOrderTradeStrategy implements OrderTradeStrategy {
	@Autowired
	private AlbumInfoFeignClient albumInfoFeignClient;
	@Autowired
	private UserInfoFeignClient userInfoFeignClient;

	@Autowired
	private StringRedisTemplate redisTemplate;
	@Override
	public OrderInfoVo trade(TradeVo tradeVo) {
		log.info("AlbumOrderTradeStrategy.trade");
		// 将信息整理成变量,方便后面使用
		Long userId = AuthContextHolder.getUserId();
		Long itemId = tradeVo.getItemId();
		// 创建返回对象
		OrderInfoVo orderInfoVo = new OrderInfoVo();
		// 获取用户信息
		UserInfo userInfo = userInfoFeignClient.findUserById(userId).getData();
		// 获取用户vip货期时间,可能为空
		Date vipExpireTime = userInfo.getVipExpireTime();

		// 获取专辑信息
		AlbumInfo albumInfo = albumInfoFeignClient.getAlbumInfo(itemId).getData();

		// 校验用户是否买过该专辑
		Boolean isUserPaidAlbum = userInfoFeignClient.isUserPaidAlbum(albumInfo.getId()).getData();
		if(isUserPaidAlbum)
			throw new GuiguException(ResultCodeEnum.REPEAT_BUY_ERROR);
		// 获取专辑单价
		BigDecimal originalAmount = albumInfo.getPrice();
		BigDecimal orderAmount = new BigDecimal("0");
		// 如果用户会员过期时间存在并且没过期,就使用会员折扣
		if(Objects.nonNull(vipExpireTime)&&(vipExpireTime.getTime()>System.currentTimeMillis())){
			BigDecimal vipDiscount = albumInfo.getVipDiscount();
			orderAmount = vipDiscount.doubleValue()>0?originalAmount.multiply(vipDiscount):originalAmount;
			orderInfoVo.setOriginalAmount(originalAmount);
			orderInfoVo.setDerateAmount(originalAmount.subtract(orderAmount));
			orderInfoVo.setOrderAmount(orderAmount);
		}else {
			// 普通用户使用普通用户折扣
			BigDecimal discount = albumInfo.getDiscount();
			orderAmount = discount.doubleValue()>0?originalAmount.multiply(discount):originalAmount;
			orderInfoVo.setOriginalAmount(originalAmount);
			orderInfoVo.setDerateAmount(originalAmount.subtract(orderAmount));
			orderInfoVo.setOrderAmount(orderAmount);
		}

		// 构造订单信息
		OrderDetailVo orderDetailVo = new OrderDetailVo();
		orderDetailVo.setItemId(itemId);
		orderDetailVo.setItemName(albumInfo.getAlbumTitle());
		orderDetailVo.setItemUrl(albumInfo.getCoverUrl());
		orderDetailVo.setItemPrice(orderAmount);
		orderInfoVo.setOrderDetailVoList(List.of(orderDetailVo));
		// 构造折扣信息
		OrderDerateVo orderDerateVo = new OrderDerateVo();
		orderDerateVo.setDerateType("1405");
		orderDerateVo.setDerateAmount(originalAmount.subtract(orderAmount));
		orderDerateVo.setRemarks("专辑折扣");
		orderInfoVo.setOrderDerateVoList(Lists.newArrayList());
		// 设置其她参数
		this.setOtherParameters(orderInfoVo,redisTemplate);
		return orderInfoVo;
	}

	@Override
	public String getItemType() {
		return SystemConstant.ORDER_ITEM_TYPE_ALBUM;
	}
}
